package com.markerhub.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.util.concurrent.RateLimiter;
import com.markerhub.common.lang.Result;

import com.markerhub.entity.Blog;
import com.markerhub.entity.Discussion;
import com.markerhub.service.DiscussionService;
import com.markerhub.util.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.logging.Logger;

@RestController
public class DiscussionController {

//    private Logger logger = LoggerFactory.getLogger(RateLimiterCotroller.class);


    @Autowired
    DiscussionService DiscussionService;

//  列表
    @GetMapping("/discussion")
    public Result list(@RequestParam(defaultValue = "1") Integer currentPage) {

        Page page = new Page(currentPage, 5);
        IPage pageData = DiscussionService.page(page, new QueryWrapper<Discussion>().orderByDesc("id"));

        return Result.succ(pageData);
    }

    @DeleteMapping("/D_delete/{id}")
    public Result delete(@PathVariable(name = "id") int id) {

        int res=DiscussionService.delete(id);

        return Result.succ(0,"删除成功",res);
    }

    //    编辑
//    @RequiresAuthentication
    @PostMapping("/discussion/edit")
    public Result edit(@Validated @RequestBody Discussion discussion) {

        Discussion temp = new Discussion();
        temp.setUserId(1L);
        temp.setCreated(LocalDateTime.now());
        temp.setStatus(0);

//      复制discussion中的内容到temp
        BeanUtil.copyProperties(discussion, temp, "id", "userId", "created", "status");
        DiscussionService.saveOrUpdate(temp);

        return Result.succ(null);
    }
}

